/* ----------------------------------------------------------------------------------------------------------------------
 * DesA
 * -----------------------------------------------------------------------------------------------------------------------
 */
package pure.utils 
{
	
	/**
	 * 数组(实用类)
	 */
public class DesA 
{
	
	/**
	 * 随机排序数组
	 * @param list
	 */
	public static function sortRandomArray(list:Array):void {
		list.sort(function(a:*, b:*):Number {
			return Math.pow( -1, Math.floor(Math.random() * 2));
		} );
	}

	/**
	 * 删除全部指定元素
	 * @param list
	 * @param obj
	 */
	public static function removeAllFromArray(list:Array, obj:Object):void {
		var len:int = list.length;
		for (var i:int = len; i >= 0; i--) {
			if (list[i] == obj) {
				list.splice(i, 1);
			}
		}
	}
	
	/**
	 * 洗牌
	 * @param list
	 * @param HowManyTimes	
	 */
	public static function shuffle(list:Array,HowManyTimes:uint):Array {
		var i:uint = 0;
		var index1:uint;
		var index2:uint;
		var object:Object;
		while(i < HowManyTimes) {
			index1 = Math.random()*list.length;
			index2 = Math.random()*list.length;
			object = list[index2];
			list[index2] = list[index1];
			list[index1] = object;
			i++;
		}
		return list;
	}
	
	/**
	 * 清除相同元素
	 * @param list
	 */
	public static function clearIdentical(list:Array):void {
		var len:int = list.length;
		for (var i:int = 0; i < len - 1; i++) {
			for (var j:int = i + 1; j < len; j++) {
				if (list[i] == list[j]) {
					list.splice(j, 1);
					j--;
					len--;
				}
			}
		}
	}
	
	
	
	
	
	
	//分裂layout queue代码分别return两个code数组
	static public function dissociate(obj:String):Array
	{
		var layout:String = obj.split('_')[0];
		var queue:String = obj.split('_')[1];
		var layoutArray:Array = [];
		var queueArray:Array = [];
		for (var i:int = 0; i < layout.length / 11; i++)
		{
			layoutArray[i] = layout.substr(i * 11, 11);
		}
		for (i = 0; i < queue.length / 8; i++)
		{
			queueArray[i] = queue.substr(i * 8 , 8);
		}
		return [layoutArray, queueArray];
	}
	//num to code for layout
	static public function makeLayoutCode(layout:Array) :String
	{
		var layoutCode:String = "";
		layoutCode += (layout[0] + num2code(layout[1]) + num2code(layout[2]) + num2code(layout[3]) + num2code(layout[4]) + num2code(layout[5]));
		return layoutCode;
	}
	//num to code for queue
	static public function makeQueueCode(queue:Array) : String
	{
		var queueCode:String = "";
		queueCode += (queue[0] + num2code(queue[1]) + num2code(queue[2]) + num2code(queue[3]) + dig2code(queue[4]));
		return queueCode;
	}
	//code to num for layout
	static public function makeLayoutArray(layoutCode:String):Array
	{
		var layoutInfo:Array=[];
		layoutInfo[0] = layoutCode.charAt(0);					//shape
		layoutInfo[1] = code2num(layoutCode.substr( 1, 2));		//width
		layoutInfo[2] = code2num(layoutCode.substr( 3, 2)); 	//height
		layoutInfo[3] = code2num(layoutCode.substr( 5, 2));		//angle
		layoutInfo[4] = code2num(layoutCode.substr( 7, 2));		//posX
		layoutInfo[5] = code2num(layoutCode.substr( 9, 2));		//posY
		return layoutInfo;
	}
	//code to num for queue
	static public function makeQueueArray(queueCode:String):Array
	{
		var queueInfo:Array = [];
		queueInfo[0] = queueCode.charAt(0);						//shape
		queueInfo[1] = code2num(queueCode.substr( 1, 2));		//width
		queueInfo[2] = code2num(queueCode.substr( 3, 2));		//height
		queueInfo[3] = code2num(queueCode.substr( 5, 2));		//angle
		queueInfo[4] = let2num(queueCode.charCodeAt( 7));		//
		return queueInfo;
	}
	
	
	
	
	
	//===========================================private==========================================
	//48~57  ----  0~9 <0-9>
	//97~122 ----  a~z <10-35>
	//65~90  ----  A~Z <36-61>
	static private function dig2code(obj:int):String
	{
		if (obj <= 9) return String(obj);							//number
		if (obj <= 35) return String.fromCharCode(obj + 97 - 10);	//lower char
		return String.fromCharCode(obj + 65 - 36);					//upper char //obj<=61
	}
	//from num to code
	static private function num2code(obj:int):String
	{
		var a:int = Math.floor(obj / 62);
		var b:int = obj % 62;
		return dig2code(a)+dig2code(b);
	}
	
	static private function let2num(obj:int):int
	{
		if (obj >= 97) return obj - 97 + 10; //lower char
		if (obj >= 65) return obj - 65 + 36;//upper char
		return obj - 48;					//number
	}
	//from code to num
	static private function code2num(obj:String):int
	{
		return 62 * let2num(obj.charCodeAt(0)) + let2num(obj.charCodeAt(1));
	}
}

}